package edu.asu.sapa.sas;

import java.util.ArrayList;
import java.util.HashMap;

public class Variable {

	protected static int numVariables = 0;
	protected int id;
	protected String name;
	protected int domain;
	protected int value;
	/** Effects associated with this variable. */
	protected ArrayList<Effect> effects = new ArrayList<Effect>();
	protected ArrayList<Prevail> prevails = new ArrayList<Prevail>();
	/** A value/atom pair from the group file */
	protected HashMap<Integer, Atom> atoms = new HashMap<Integer, Atom>();
	
	public Variable(String name, int domain, int axiomLayer) {
		this.name = name;
		this.domain = domain;
		id = numVariables++;
		value = -1; // undefined
	}

	public int getID() {
		return id;
	}
	
	public void setValue(int val) {
		value = val;
	}
	
	public int getValue() {
		return value;
	}
	
	public int getDomain() {
		return domain;
	}
	
	public boolean addEffect(Effect e) {
		return effects.add(e);
	}
	
	public boolean addPrevail(Prevail p) {
		return prevails.add(p);
	}
	
	/**
	 * Insert an atom with a value of val.
	 * @param val The value of the atom within this variable.
	 * @param atom The atom to be added.
	 * @return If this atom is replaced by another, then this will return
	 *   the old valued atom.  Otherwise, it will return null.
	 */
	public Atom addAtom(Integer val, Atom atom) {
		return atoms.put(val, atom);
	}
	
	public Atom getAtom(Integer val) {
		return atoms.get(val);
	}
	
	public ArrayList<Effect> getEffects() {
		return effects;
	}

	public ArrayList<Prevail> getPrevails() {
		return prevails;
	}
	
	public String getName() {
		return name;
	}
	
	public int getNoneValue() {
		return atoms.size();
	}
	
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(name + "={");
		sb.append(0);
		for (int i=1;i<domain;i++) {
			sb.append(",").append(i);
		}
		sb.append("}");
		return sb.toString();
	}
}
